package com.module.retrofit.coverter;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.module.retrofit.exception.ResponseException;


import java.io.IOException;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Converter;

/**
 * Created by dell on 2017/5/10
 */

public class JsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {

    private final Gson gson;
    private final TypeAdapter<T> adapter;

    JsonResponseBodyConverter(Gson gson, TypeAdapter<T> adapter) {
        this.gson = gson;
        this.adapter = adapter;
    }


    @Override
    public T convert(ResponseBody responseBody) throws IOException {
//        JsonReader jsonReader = gson.newJsonReader(responseBody.charStream());
//        try {
//            T result = adapter.read(jsonReader);
//            if (jsonReader.peek() != JsonToken.END_DOCUMENT) {
//                throw new JsonIOException("JSON document was not fully consumed.");
//            }
//            return result;
//        } finally {
//            responseBody.close();
//        }
        String body = responseBody.string();
        if(body.contains("<!DOCTYPE html>")){
            return (T) body;
        }
        JSONObject json = JSON.parseObject(body);
        String error = json.getString("error");
        String errorMsg = json.getString("errorMsg");
        if (!"0".equals(error)) {
            throw new ResponseException(error, errorMsg);
        }
        //修改部分参数
        String newJson = addImgUrl(body);
        return adapter.fromJson(newJson);
    }



    private String addImgUrl(String body){
        JSONObject jsonObject = JSON.parseObject(body);
        JSONObject data = jsonObject.getJSONObject("data");
        //过滤IndexImg
        if(data.containsKey("IndexImg") && data.containsKey("Lists")){
            Map<String, String> imdexImg = data.getObject("IndexImg", Map.class);
            JSONArray list = data.getJSONArray("Lists");
            for(int i=0;i<list.size();i++){
                JSONObject object = list.getJSONObject(i);
                String imgID = object.getString("ImgID");
                object.put("ImgUrl", imdexImg.get(imgID));
            }
        }
        //过滤IndexImgs
        if(data.containsKey("IndexImgs") && data.containsKey("Lists")){
            Map<String, String>  imdexImg = data.getObject("IndexImgs", Map.class);
            JSONArray list = data.getJSONArray("Lists");
            for(int i=0;i<list.size();i++){
                JSONObject object = list.getJSONObject(i);
                String imgId = object.getString("ImgId");
                object.put("ImgUrl", imdexImg.get(imgId));
            }
        }
        //过滤CardImgs
        if(data.containsKey("CardImgs") && data.containsKey("Data")){
            Map<String, Map<String, String>>  cardImgs = data.getObject("CardImgs", Map.class);
            JSONObject data2 = data.getJSONObject("Data");
            if(data2.containsKey("CardInfo")){
                JSONObject cardInfo = data2.getJSONObject("CardInfo");
                String imgId = cardInfo.getString("ImgId");
                if(cardImgs.get(imgId)!=null){
                    cardInfo.put("ImgUrl", cardImgs.get(imgId).get("Url"));
                }
            }else if(data2.containsKey("Lists")){
                JSONArray list = data2.getJSONArray("Lists");
                for(int i=0;i<list.size();i++){
                    JSONObject object = list.getJSONObject(i);
                    String imgId = object.getString("ImgId");
                    if(cardImgs.get(imgId)!=null){
                        object.put("ImgUrl", cardImgs.get(imgId).get("Url"));
                    }
                }
            }
        }
        //过滤SubServer
        if(data.containsKey("SubServer") && data.containsKey("Specs")){
            JSONArray subServer = data.getJSONArray("SubServer");
            JSONArray specs = data.getJSONArray("Specs");
            for(int i=0;i<specs.size();i++){
                JSONObject object = specs.getJSONObject(i);
                String specsId = object.getString("Id");
                JSONArray sub = object.getJSONArray("Sub");
                for(int j=0;j<sub.size();j++){
                    JSONObject object1 = sub.getJSONObject(j);
                    String subId = object1.getString("Id");
                    for(int k=0;k<subServer.size();k++){
                        JSONObject object2 = subServer.getJSONObject(k);
                        String subServerIds = object2.getString("SubServerIds");
                        String SspId = object2.getString("SspId");
                        String Price = object2.getString("Price");
                        if(subServerIds.contains(",")){
                            String id1 = subServerIds.split(",")[0];
                            String id2 = subServerIds.split(",")[1];
                            if(specsId.equals(id1) && subId.equals(id2)){
                                object1.put("SspId", SspId);
                                object1.put("Price", Price);
                            }
                        }
                    }
                }
            }
        }
        return jsonObject.toJSONString();
    }


}
